package com.uls.ldcm.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.uls.ldcm.domain.Contracts;
import com.uls.ldcm.service.IContractsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.ldcm.domain.Salaryschemes;
import com.uls.ldcm.service.ISalaryschemesService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 薪资方案Controller
 * 
 * @author gw
 * @date 2024-10-31
 */
@RestController
@RequestMapping("/ldcm/salaryschemes")
public class SalaryschemesController extends BaseController
{
    @Autowired
    private ISalaryschemesService salaryschemesService;

    @Autowired
    private IContractsService contractsService;

    /**
     * 查询薪资方案列表
     */
    @PreAuthorize("@ss.hasPermi('ldcm:salaryschemes:list')")
    @GetMapping("/list")
    public TableDataInfo list(Salaryschemes salaryschemes)
    {
        startPage();
        List<Salaryschemes> list = salaryschemesService.selectSalaryschemesList(salaryschemes);
        return getDataTable(list);
    }

    /**
     * 导出薪资方案列表
     */
    @PreAuthorize("@ss.hasPermi('ldcm:salaryschemes:export')")
    @Log(title = "薪资方案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Salaryschemes salaryschemes)
    {
        List<Salaryschemes> list = salaryschemesService.selectSalaryschemesList(salaryschemes);
        ExcelUtil<Salaryschemes> util = new ExcelUtil<Salaryschemes>(Salaryschemes.class);
        util.exportExcel(response, list, "薪资方案数据");
    }

    /**
     * 获取薪资方案详细信息
     */
    @PreAuthorize("@ss.hasPermi('ldcm:salaryschemes:query')")
    @GetMapping(value = "/{salaryschemeId}")
    public AjaxResult getInfo(@PathVariable("salaryschemeId") Long salaryschemeId)
    {
        return success(salaryschemesService.selectSalaryschemesBySalaryschemeId(salaryschemeId));
    }

    /**
     * 新增薪资方案
     */
    @PreAuthorize("@ss.hasPermi('ldcm:salaryschemes:add')")
    @Log(title = "薪资方案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Salaryschemes salaryschemes)
    {
        return toAjax(salaryschemesService.insertSalaryschemes(salaryschemes));
    }

    /**
     * 修改薪资方案
     */
    @PreAuthorize("@ss.hasPermi('ldcm:salaryschemes:edit')")
    @Log(title = "薪资方案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Salaryschemes salaryschemes)
    {
        return toAjax(salaryschemesService.updateSalaryschemes(salaryschemes));
    }

    /**
     * 删除薪资方案
     */
    @PreAuthorize("@ss.hasPermi('ldcm:salaryschemes:remove')")
    @Log(title = "薪资方案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{salaryschemeIds}")
    public AjaxResult remove(@PathVariable Long[] salaryschemeIds)
    {
        List<Contracts> contractsList = new ArrayList<>();
        for (Long salaryschemeId:salaryschemeIds){
            Contracts contracts = new Contracts();
            contracts.setSalaryschemeId(salaryschemeId);
            List<Contracts> contractsList1 = contractsService.selectContractsList(contracts);
            contractsList.addAll(contractsList1);
        }
        if(contractsList.size()>0){
            return warn("该薪资方案下有合同，无法删除");
        }
        return toAjax(salaryschemesService.deleteSalaryschemesBySalaryschemeIds(salaryschemeIds));
    }
}
